package cn.ydxiaoshuai.modules.liteuser.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ydxiaoshuai.common.api.vo.Result;
import cn.ydxiaoshuai.common.aspect.annotation.AutoLog;
import cn.ydxiaoshuai.common.system.base.controller.JeecgController;
import cn.ydxiaoshuai.common.system.query.QueryGenerator;
import cn.ydxiaoshuai.modules.liteuser.entity.LiteUserInfo;
import cn.ydxiaoshuai.modules.liteuser.entity.UserCertificate;
import cn.ydxiaoshuai.modules.liteuser.service.ILiteUserInfoService;
import cn.ydxiaoshuai.modules.liteuser.service.UserCertificateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 微信用户信息表
 * @Author: 小帅丶
 * @Date:   2020-05-14
 * @Version: V1.0
 */
@Slf4j
@Api(tags="微信用户信息表")
@RestController
@RequestMapping("/liteuser/liteUserInfo")
public class LiteUserInfoController extends JeecgController<LiteUserInfo, ILiteUserInfoService> {
	@Autowired
	private ILiteUserInfoService liteUserInfoService;

	 @Autowired
	 private UserCertificateService userCertificateService;

	/**
	 * 分页列表查询
	 *
	 * @param liteUserInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "微信用户信息表-分页列表查询")
	@ApiOperation(value="微信用户信息表-分页列表查询", notes="微信用户信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LiteUserInfo liteUserInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LiteUserInfo> queryWrapper = QueryGenerator.initQueryWrapper(liteUserInfo, req.getParameterMap());
		Page<LiteUserInfo> page = new Page<LiteUserInfo>(pageNo, pageSize);
		IPage<LiteUserInfo> pageList = liteUserInfoService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * @author jiangt 230818
	  * @param userId
	  * @return
	  */
	 @ApiOperation(value = "微信个人信息获取(基础信息+认证信息)", notes = "微信个人信息获取(基础信息+认证信息)")
	 @ApiImplicitParams({
			 @ApiImplicitParam(required = true, name = "userId", value = "用户id"),
	 })
	 @RequestMapping(value = "/getwxuser", method = {RequestMethod.GET})
	 public Map<String,Object> getWxUser(@RequestParam(name = "userId", defaultValue = "") String userId) {

		 //查询用户表
		 LambdaQueryWrapper<LiteUserInfo> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(LiteUserInfo::getUserId, userId);
		 LiteUserInfo liteUser = liteUserInfoService.getOne(queryWrapper);

		 //查询证书
		 LambdaQueryWrapper<UserCertificate> certificate = new LambdaQueryWrapper<>();
		 certificate.eq(UserCertificate::getUserId, userId);
		 List<UserCertificate> certificates = userCertificateService.list(certificate);

		 //返回结果包装
		 Map<String,Object> map=new HashMap<>();
		 map.put("liteUser",liteUser);
		 map.put("certificates",certificates);
		 map.put("status",200);
		 return map;
	 }


	 /**
	 * 添加
	 *
	 * @param liteUserInfo
	 * @return
	 */
	@AutoLog(value = "微信用户信息表-添加")
	@ApiOperation(value="微信用户信息表-添加", notes="微信用户信息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LiteUserInfo liteUserInfo) {
		liteUserInfoService.save(liteUserInfo);
		return Result.ok("添加成功！");
	}

	 /**
	  * 编辑
	  *
	  * @param liteUserInfo
	  * @return
	  */
	 @AutoLog(value = "微信用户信息表-编辑")
	 @ApiOperation(value="微信用户信息表-编辑", notes="微信用户信息表-编辑")
	 @PutMapping(value = "/updateByUserId")
	 public Result<?> updateByUserId( LiteUserInfo liteUserInfo) {

		 //查询用户表
		 liteUserInfoService.updateByUserId(liteUserInfo.getUserId(),liteUserInfo.getNickName(),
				 liteUserInfo.getAvatarUrl(),liteUserInfo.getPhoneNum());
		 List<LiteUserInfo>  liteUsers = liteUserInfoService.getByUserId(liteUserInfo.getUserId());
		 return Result.ok(liteUsers);
	 }

	/**
	 * 编辑
	 *
	 * @param liteUserInfo
	 * @return
	 */
	@AutoLog(value = "微信用户信息表-编辑")
	@ApiOperation(value="微信用户信息表-编辑", notes="微信用户信息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LiteUserInfo liteUserInfo) {
		liteUserInfoService.updateById(liteUserInfo);
		return Result.ok("编辑成功!");
	}
	 /**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "微信用户信息表-通过id删除")
	@ApiOperation(value="微信用户信息表-通过id删除", notes="微信用户信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		liteUserInfoService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "微信用户信息表-批量删除")
	@ApiOperation(value="微信用户信息表-批量删除", notes="微信用户信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.liteUserInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "微信用户信息表-通过id查询")
	@ApiOperation(value="微信用户信息表-通过id查询", notes="微信用户信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LiteUserInfo liteUserInfo = liteUserInfoService.getById(id);
		return Result.ok(liteUserInfo);
	}

	 /**
	  * 通过id查询
	  *
	  * @param userId
	  * @return
	  */
	 @AutoLog(value = "微信用户信息表-通过userId查询")
	 @ApiOperation(value="微信用户信息表-通过id查询", notes="微信用户信息表-通过id查询")
	 @GetMapping(value = "/queryUserId")
	 public Result<?> queryUserId(@RequestParam(name="userId",required=true) String userId) {
		 List<LiteUserInfo> liteUserInfo = liteUserInfoService.getByUserId(userId);
		 return Result.ok(liteUserInfo.get(0));
	 }
  /**
   * 导出excel
   *
   * @param request
   * @param liteUserInfo
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, LiteUserInfo liteUserInfo) {
      return super.exportXls(request, liteUserInfo, LiteUserInfo.class, "微信用户信息表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, LiteUserInfo.class);
  }

}
